package Service.TeacherService;

import Dao.TeacherDao.TeacherSQLdao;
import Entity.Course;
import Entity.SelectCourse;
import Entity.Teacher;
import Util.RandomID;

import java.util.HashSet;
import java.util.List;
import java.util.Random;
/*
教师后台 Service层 负责整合Dao层逻辑
 */
public class TeacherService {

    private TeacherSQLdao teacherSQLdao = new TeacherSQLdao();


    /*
    教师注册
    逻辑 先判断是否已经有该学号  如果有则不注册
     */
    public int teacherRegister(Teacher teacher){
        if (queryIdIsExist(teacher.getTeacherId())>0) return 0;
        return  teacherSQLdao.insertTeacher (teacher);
    }



    /*
    查询该教师Id是否存在 注册前判断
     */
    public int queryIdIsExist(String teacherId){return  teacherSQLdao.queryIdIsExist(teacherId);}



    /*
    教师登陆
     */
    public Teacher teacherLogin(Teacher teacher){return teacherSQLdao.selectTeacherByTeacherIdAndPassword(teacher);}



    /*
    教师修改个人信息 并更新数据
    逻辑 如果修改成功 得返回已经修改成功的数据
         否则返回空
     */
    public Teacher modifyPasswordById(Teacher teacher){
        if (teacherSQLdao.modifyPassword(teacher)>0){
            return teacherSQLdao.selectTeacherNoPassword(teacher);
        }
        return null;
    }




    /*
    教师修改个人密码 并更新数据
    逻辑 如果修改成功 得返回已经修改成功的数据
         否则返回空
     */
    public Teacher updateByTeacherId(Teacher teacher){

        if(teacherSQLdao.updateByTeacherId(teacher)>0){
            return teacherSQLdao.selectTeacherNoPassword(teacher);
        }
        return null;
    }



    /*
    教师添加(发布)新的课程 需要设置课程状态 0
     */
    public int insertCourseInfo(Course course) {

        // 生成随机CourseId;
        String courseId = RandomID.getRandomChar(8);

        course.setCourseId(courseId);

        //设置需要状态
        course.setStatus(0);

        return teacherSQLdao.insertCourseInfo(course);
    }



    /*
    教师删除发布的课程
    逻辑 遍及课程列表 并调用Dao层删除数据
     */
    public int deleteTeacherCourse(List<Course> courseList){
        int count = 0;
        for (int i = 0;i<courseList.size();i++){
            Course course = courseList.get(i);
            count = count + teacherSQLdao.deleteTeacherCourse(course);
        }
        return  count;
    }


    /*
    教师修改学生成绩
     */
    public int updateStudentScore(SelectCourse selectCourse){
        return teacherSQLdao.updateStudentScore(selectCourse);
    }



    /*
    教师通过教师ID 教师姓名 查询是否有此人
     */
    public boolean selectTeacherByIdAndName(String teacherId, String teacherName){return  teacherSQLdao.selectTeacherByIdAndName(teacherId,teacherName);}




    /*
    教师通过 教师ID 教师姓名  新的教师密码 修改密码
     */
    public int updatePasswordNoLogin(String teacherId,String teacherName, String teacherPassword){return teacherSQLdao.updatePasswordNoLogin(teacherId,teacherName,teacherPassword);}



}
